

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../../cache/fm_cache_tool.dart';
import '../../commonTool/fm_jump_tool.dart';
import '../../commonUI/fm_progress_hud.dart';
import '../../commonUI/fm_toast.dart';
import '../../net/fm_net_param_manager.dart';
import 'fm_cancel_account_api.dart';
import 'fm_logout_api.dart';

class FMSettingController extends GetxController {

  bool isClickProtocol = false;

  void logout(BuildContext context) {
    FMLogoutApi api = FMLogoutApi();

    FMProgressHud.showLoading();

    api.send().then((value){

      FMProgressHud.hideLoading();

      if (value.code == 0) {
        FMNetParamManager().setAbsence("sessionId");
        FMCacheTool.saveSessionId("");
        Navigator.pop(context);
        Navigator.pop(context);
        FMJumpTool.jumpHomeWidget();
      }else {
        FMToast.showText(value.message ?? "Network Connection Error");
      }
    });
  }

  void accountCancel(BuildContext context) {
    if (!isClickProtocol) {
      FMToast.showText("Please read and agree to the Privacy Policy");
      return;
    }
    FMProgressHud.showLoading();

    FMCancelAccountApi api = FMCancelAccountApi();
    api.send().then((value){
      FMProgressHud.hideLoading();
      if (value.code == 0) {
        FMNetParamManager().setAbsence("sessionId");
        FMCacheTool.saveSessionId("");
        Navigator.pop(context);
        Navigator.pop(context);
        FMJumpTool.jumpHomeWidget();
      }else {
        FMToast.showText(value.message ?? "Network Connection Error");
      }
    });
  }

  updateProtocol() {
    isClickProtocol = !isClickProtocol;
    update(["updateProtocol"]);
  }
}